import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Scanner;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

import fwCedam.general.Fichiers;
import fwCedam.utilitaire.Chronometre;

public class GenererSon {
	
	private SourceDataLine line = null;
	
	public static int[][] frequenceNotes =	{ 
									{32,65,131,261,523,1046,2093,4186},
	                                {36,73,147,293,587,1174,2349,4698},
	                                {41,82,165,329,659,1318,2637,5274},
	                                {43,87,174,349,698,1397,2793,5587},
	                                {49,98,196,392,784,1568,3135,6271},
	                                {55,110,220,440,880,1760,3520,7040},
	                                {61,123,247,493,987,1975,3951,7902}
								};

	
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		GenererSon gene = new GenererSon();
		while (true) {
			
			Scanner sc = new Scanner(System.in);
			System.out.println("Veuillez saisir un mot :");
			String str = sc.nextLine();
			int valeur = new Double((new Double(str)*1000)).intValue();
			System.out.println(valeur);
			gene.genererSon(valeur);
		}
		//System.exit(0);

	}
	
	public void genererSon(int frequence) {
	
		genererSonThread thread = new genererSonThread(frequence);
		thread.start();
	}
	
	private class genererSonThread extends Thread{
		
		int frequence;
		
		genererSonThread(int frequence) {
			this.frequence = frequence;
		}
		
		
		@Override
		public void run() {
			genererSon(frequence);		
		}
		
		
		
		
		public void genererSon(int frequence) {
			byte[] signal = genereSinus(frequence);
			Chronometre temps = new Chronometre();
			temps.demarrer();
			sortieAudio(signal);
			temps.arreter();
			//ecrireFichier(signal);
			System.out.println("Duree : "+temps.getDuree());
			System.out.println();
		}
		
		private byte[] genereSinus(int frequence) {
			byte[] signal = new byte[7000];
			Double coeff = new Double(new Double(frequence)/22.5);
			for (int i = 0; i < signal.length; i++) {
				Double ii = Math.toRadians(i*coeff);
				//System.out.println(i+"	"+ii+"	"+String.valueOf(Math.sin(ii)).replace(".", ","));
				Double sinUn = Math.sin(ii)*64;
				Double coef = null;
				coef = new Double(1);
				if(i>1000) {
					coef = 0.6;
				}
				if(i>3000) {
					coef = 0.4;
				}
				if(i>5000) {
					coef = new Double(0.1);
				}
				Double sinDeux = sinUn*coef;
				signal[i] = sinDeux.byteValue();
			}
			return signal;
		}
		
		
		/**
		 * Renvoi le signal vers la sortie audio (Haut-parleur)
		 * @param entree
		 */
		boolean isEnCours = false;
		private void sortieAudio(byte[] entree) {

			if(!isEnCours) {
				isEnCours=true;
				SourceDataLine line = getAudio();
				line.start(); 
				line.write(entree, 0, entree.length);
				line.stop();
				isEnCours = false;
			}
				
		}

		private SourceDataLine getAudio() {
			
			//if(line==null) {
			    AudioFormat format = initAudioFormat(); 
			    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); 
			    
				try {
					line = (SourceDataLine) AudioSystem.getLine(info);
				    line.open(format); 
				} catch (LineUnavailableException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			//}
		    
		    return line;
		}
		
		private AudioFormat initAudioFormat() {
			float sampleRate = 8000; 
			int sampleSizeInBits = 8; 
			int channels = 1; 
			boolean signed = true; 
			boolean bigEndian = true; 
			AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
			return format;
		}
		
		private void ecrireFichier(byte[] signal) {
			StringBuilder txt = new StringBuilder();
			for (int i = 0; i < signal.length; i++) {
				txt.append(signal[i]).append("\n");
			}
			
			Fichiers fichier = new Fichiers("signal.xls", true);
			try {
				fichier.ecrirerFichier(txt.toString());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		
		
		
		
		
		
	}
	
	
	
}
